<?

//require_once(dirname(__FILE__) . '/../log.php');

abstract class bf_db {

	protected $opts;
	protected $log;
	protected $initialized;

	public function __construct($options = array()) {

		$this->opts = array_merge(array(
			'host' => 'localhost',
			'database' => 'dbname',
			'user' => 'dbuser',
			'password' => '',
			'log' => dirname(__FILE__) . '/../../logs/db-error.log',
			'logging' => 1,
		), $options);

		$this->log = new bf_log($this->opts['log']);

		$this->initialized = false;
	}

	public function __destruct() {
		if ($this->initialized) {
			$this->disconnect();
		}
	}

	protected function init() {
		if (!$this->initialized) {
			$this->connect();
			$this->initialized = true;
		}
	}

	abstract public function escape($str);

	abstract public function connect();
	abstract public function disconnect();
	abstract public function is_connected();

	abstract function get_true();
	abstract function get_false();
	abstract function is_true($value);
	abstract function is_false($value);

	abstract protected function make_query($sql, $params = array());
	abstract public function fetch_all($res);
	abstract public function fetch_row($res);
	abstract public function fetch_column($res, $column = false);

	abstract public function is_error();
	abstract public function get_error_text();
	abstract public function get_debug_text();

	abstract public function start();
	abstract public function commit();
	abstract public function rollback();

	protected function log_append($msg) {
		$ts = date('Y-m-d H:i:s');
		$hn = 'server-name: ' . @$_SERVER['SERVER_NAME'] . ' / http-host: ' . @$_SERVER['HTTP_HOST'];
		$this->log->append("{\n\n$ts $hn\n\n$msg\n}\n");
	}

	protected function log_error() {
		if ($this->opts['logging'] > 0) {
			if ($this->is_error())
				$this->log_append($this->get_error_text());
		}
	}

	public function fetch_all_obj($res) {
		$rows = array();
		while ($row = $this->fetch_obj($res))
			$rows[] = $row;
		return $rows;
	}

	public function fetch_obj($res) {
		if ($row = $this->fetch_row($res))
			return (object) $row;
		return false;
	}

	public function query($sql, $params = array()) {
		$this->init();
		if (!is_array($params))
			$params = array($params);
		return $this->make_query($sql, $params);
	}

	public function query_obj($sql, $params = array()) {
		$this->init();
		return $this->fetch_obj($this->query($sql, $params));
	}

	public function query_row($sql, $params = array()) {
		$this->init();
		return $this->fetch_row($this->query($sql, $params));
	}

	public function query_column($sql, $params = array(), $column = false) {
		$this->init();
		return $this->fetch_column($this->query($sql, $params), $column);
	}

	public function query_all($sql, $params = array()) {
		$this->init();
		return $this->fetch_all($this->query($sql, $params));
	}

	public function query_all_obj($sql, $params = array()) {
		$this->init();
		return $this->fetch_all_obj($this->query($sql, $params));
	}
}

?>